package com.dotstone.chipism.view;

import java.security.KeyStore.TrustedCertificateEntry;

import com.dotstone.chipism.listener.NavigationBarChangedListener;
import com.dotstone.chipism.util.Util;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;

public class MyLinearLayout extends LinearLayout {
	private Context c;
	private int lastH;
	private int currentH;
	private NavigationBarChangedListener listener;
	private boolean needOn = true;

	public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		c = context;
		// TODO Auto-generated constructor stub
	}

	public MyLinearLayout(Context context) {
		super(context);
		c = context;
		// TODO Auto-generated constructor stub
	}

	public MyLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		c = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// Log.i("wmy", getWidth() + " " + getHeight());
		lastH = getHeight();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		// Log.i("wmy", "MyLinearLayout onLayout");
		// Log.i("wmy", changed + " " + l + " " + t + " " + r + " " + b);
		currentH = b;
		// Log.i("wmy", "lastH = " + lastH + " currentH = " + currentH);
		if (lastH != 0) {
			if (currentH > lastH) {
				if (listener != null) {
					if (Util.getNavigationBarHeight(c) == (currentH - lastH)) {
						listener.show(false);
						Log.i("nav", "隐藏导航栏 调用接口" + "lastH = " + lastH + " currentH = " + currentH);
					}
				}
			} else if (currentH < lastH) {
				if (listener != null) {
					if (Util.getNavigationBarHeight(c) == (lastH - currentH)) {
						listener.show(true);
						Log.i("nav", "显示导航栏 调用接口" + "lastH = " + lastH + " currentH = " + currentH);
					}
				}
			}
		}
	}

	public void setNeedOn(boolean needOn) {
		this.needOn = needOn;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
	}

	// interface NavigationBarChangedListener {
	// public void show(boolean show);
	// }

	public void setNavigationBarChangedListener(NavigationBarChangedListener listener) {
		this.listener = listener;
	}

}
